STATA中的Markdown转换命令markstat

您所在的位置:网站首页 stata ssc install导出word STATA中的Markdown转换命令markstat

STATA中的Markdown转换命令markstat

2023-08-15 03:14| 来源: 网络整理| 查看: 265

% Stata Markdown % wintryheart % 2019年7月1日

STATA Markdown 前言

本文基于markstat命令的开发者Germán Rodríguez的使用说明整理。原文请见 GR的Stata Markdown主页。

0、准备

STATA 15开始支持Markdown。个人认为markstat比STATA15自带的markdown命令更好用。

我的STATA版本是14,需要安装两个命令和一些外部程序。

1)安装两个命令: markstat 用于执行包含stata和markdown代码的动态文件。

ssc install markstat

whereis 用于指定外部程序和辅助文件的路径目录。

**ssc install whereis **

2)安装外部程序 Pandoc 用于把Markdown转换成HTML,PDF,或DOCX的程序。

  首先,请去Pandoc的官网下载。

  安装好后,打开STATA,使用whereis命令指定Pandoc程序的位置。

whereis pandoc "c:/program files/pandoc/pandoc.exe"

R 如果想包含R代码,需要先安装R,然后用whereis指定R程序位置。

whereis R "c:/program files/R/R-3.4.3/R/bin/x64/R.exe"

Latex 可选。操作同上类似。但我倾向于用Pandoc转成HTML,然后用浏览器转成PDF。 2、编写markstat文件(.stmd)

注意:markstat使用{{n}}作为stata代码的预留块的标记,所以在编写markstat文件时, 不要使用双大括号。

1)包含stata代码的最简单的文件

Stata代码只需要缩进 1个tab或4个空格。如下例:

Let's read the fuel efficiency data that comes with Stata, compute gallons per 100 miles, and regress that on weight

   sysuse auto, clear    gen gphm = 100/mpg    regress gphm weight

We see that heavier cars use more fuel, with 1,000 pounds requiring an extra 1.4 gallons to travel 100 miles.

Let's read the fuel efficiency data that comes with Stata, compute gallons per 100 miles, and regress that on weight

sysuse auto, clear gen gphm = 100/mpg regress gphm weight

We see that heavier cars use more fuel, with 1,000 pounds requiring an extra 1.4 gallons to travel 100 miles.

2)Markdown

Markstat支持通用的markdown语法。也支持HTML语法,但是如果要直接生成PDF文件, 建议不要插入HTML语法。

3)数学公式 支持$ $ 行内插入公式。 支持$$ $$ 插入公式块。 输出漂亮的公式,建议执行markstat命令时添加mathjax选项。 公式块,建议缩进输入公式。不用担心会和stata命令相混淆。 $$ y = \alpha + \beta x + e $$

输出结果为:

y = \alpha + \beta x + e

4)Metadata 文件标记

Pandoc支持文件的标题、作者和日期作为metadata。

注意,必须在文件开头第一行开始用%标注相关信息。

例如:

% Stata Markdown % Germán Rodríguez % 26 October 2016 5)代码块Fenced Code Blocks

前面提到用“1个tab或4个空格”的规则来区分STATA和Markdown的代码。

规范做法最好用代码块。

a) STATA代码块 ```s //STATA code goes here ```

或者

```{s} //STATA code goes here ```

如果不返回命令

```s/ 或者 {s/} //STATA code goes here ``` 6)行内代码(Inline code)

行内代码的语法:

`s [fmt] expression` // fmt是可选格式

例如:

quietly sysuse auto, clear quietly gen gphm = 100/mpg quietly regress gphm foreign

The R-squared of the regression is ` s e(r2) `, or ` s%5.2f e(r2) `。

如果要在行内引用R代码的结果,语法类似为` r expression `。注意,这里没有fmt选项。如果对结果格式化,可以使用round()函数。例如

library(haven) auto


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3